import 'package:fake_handwriting/Constant.dart';
import 'package:fake_handwriting/Util.dart';
import 'package:fake_handwriting/generated/l10n.dart';
import 'package:fake_handwriting/http/http.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class Login extends StatefulWidget {
  const Login({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => LoginPage();
}

class LoginPage extends State<Login> {
  final _email_controller = TextEditingController();
  final _password_controller = TextEditingController();

  var check = false;
  @override
  void dispose() {
    super.dispose();
    _email_controller.dispose();
    _password_controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
            child: Column(
              children: [
                TextField(
                  controller: _email_controller,
                  inputFormatters: [
                    Constant.input_filter_email,
                  ],
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(Radius.circular(10)),
                      ),
                      labelText: S.of(context).email,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputEmail,
                      prefixIcon: const Icon(
                        Icons.mail_outline,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                TextField(
                  obscureText: true,
                  controller: _password_controller,
                  cursorColor: Colors.blue,
                  decoration: InputDecoration(
                      enabledBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(const Radius.circular(10)),
                      ),
                      focusedBorder: const OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.blue),
                        borderRadius: BorderRadius.all(const Radius.circular(10)),
                      ),
                      labelText: S.of(context).password,
                      labelStyle: const TextStyle(color: Colors.blue),
                      hintText: S.of(context).inputPassword,
                      prefixIcon: const Icon(
                        Icons.password_rounded,
                        color: Colors.blue,
                      )),
                ),
                const SizedBox(
                  height: 20,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  child: InkWell(
                    onTap: () async {
                      if (_email_controller.text.isNotEmpty){
                        if (_password_controller.text.isNotEmpty) {
                          if (check) {
                            try {
                              var token = await Http.login(
                                  _email_controller.text,
                                  _password_controller.text);
                              await Util.loginSuccess(token);
                              Util.toast(S.of(context).loginSuccessful);
                              Navigator.pop(context);
                            } catch (e) {
                              print(e);
                            }
                          } else {
                            Util.toast(S.of(context).policyCheck);
                          }
                        } else {
                          Util.toast(S.of(context).inputPassword);
                        }
                      } else {
                        Util.toast(S.of(context).inputEmail);
                      }
                    },
                    child: Container(
                      padding: const EdgeInsets.symmetric(vertical: 15),
                      width: double.infinity,
                      decoration: const BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.all(Radius.circular(5))),
                      child: Center(
                        child: Text(
                          S.of(context).login,
                          style: const TextStyle(color: Colors.white),
                        ),
                      ),
                    ),
                  ),
                ),
                Row(
                  children: [
                    Checkbox(
                        value: check,
                        onChanged: (v) {
                          check = v!;
                          setState(() {});
                        }),
                    // const Align(alignment: Alignment.centerLeft,child: Text("和")),
                    SingleChildScrollView(
                      scrollDirection: Axis.vertical,
                      child: Text.rich(
                        TextSpan(
                          children: [
                            TextSpan(
                              text: S.of(context).iHaveRead,
                              style: const TextStyle(
                                color: Colors.black,
                              ),
                            ),
                            TextSpan(
                              text: '《${S.of(context).privacyPolicy}》',
                              style: const TextStyle(
                                color: Colors.blue,
                              ),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () => Navigator.pushNamed(
                                    context, "web",
                                    arguments:
                                        "${Http.BASE_URL}policy/?type=0"),
                            ),
                            const TextSpan(
                              text: '&',
                              style: TextStyle(
                                color: Colors.black,
                              ),
                            ),
                            TextSpan(
                              text: '《${S.of(context).serviceItems}》',
                              style: const TextStyle(
                                color: Colors.blue,
                              ),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () => Navigator.pushNamed(
                                    context, "web",
                                    arguments:
                                        "${Http.BASE_URL}policy/?type=1"),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                const Divider(),
                Row(
                  children: [
                    InkWell(
                        onTap: () {
                          Navigator.pushNamed(context, "register");
                        },
                        child: Padding(
                            padding: const EdgeInsets.symmetric(
                                vertical: 20, horizontal: 40),
                            child: Text(S.of(context).register))),
                    InkWell(
                        onTap: () {
                          Navigator.pushNamed(context, "find_password");
                        },
                        child: Padding(
                            padding: const EdgeInsets.symmetric(
                                vertical: 20, horizontal: 40),
                            child: Text(S.of(context).retrievePassword))),
                  ],
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                )
              ],
            ),
          ),
        ),
      ),
      appBar: AppBar(
        title: Text(S.of(context).login),
      ),
    );
  }
}
